﻿namespace ScrumLite.WebUx.Controllers
{
    using System.Web.Mvc;
    using ScrumLite.Services;
    using ScrumLite.WebUx.Models;
    using System.Collections.ObjectModel;
    using ScrumLite.Services.Contracts;
    using System.Collections.Generic;
    using System.Linq;
    using System;
    using GoogleChartSharp;
    using GoogleChartSharp.CustomCharts;
    using ScrumLite.Gateways;
    using System.Configuration;
    using System.Globalization;

    public class WorkAreasController : ContextBaseController
    {
        IRequestsService requestsService;

        public WorkAreasController() : this(new RequestsServiceClient(), new ProjectsServiceClient(), new ProfileServiceClient())
        {
        }

        public WorkAreasController(IRequestsService requests, IProjectsService projectService, IProfileService profileService)
            : base (profileService,projectService)
        {
            this.requestsService = requests;
        }

        public void List(string projectId)
        {
            WorkAreasListViewData data = new WorkAreasListViewData(new Collection<WorkAreaWeight>(requestsService.GetWorkAreasGroupByWeight(projectId).ToList()));

            data.ProjectName = Projects.GetProjectName(projectId);
            data.ProjectId = projectId;
            data.ViewTitle = "Work Areas";
            RenderView("List", data);
        }

        public void New(string projectId)
        {
            ShowWorkArea(String.Empty, Guid.NewGuid().ToString(),projectId);
        }

        public void Edit(string projectId, string name, string id)
        {
            ShowWorkArea(Decoder(name), id,projectId);
        }

        private void ShowWorkArea(string name, string id, string projectId)
        {
            WorkAreaViewData data = new WorkAreaViewData();
            data.Name = name;
            data.Id = new Guid(id);
            data.ProjectName = Projects.GetProjectName(projectId);
            data.ProjectId = projectId;
            data.ViewTitle = "Add or Edit Work Area";
            RenderView("Edit", data);
        }
        
        public void Save(string projectId, string name, string id)
        {
            WorkArea wa = new WorkArea();
            wa.Name = name;
            wa.Id = id;
            requestsService.SaveWorkArea(wa, new Guid(projectId));
            RedirectToAction(new { Action = "List", Controller = "WorkAreas", projectId=projectId});
        }

        public void WorkAreasMetrics(string projectId)
        {
            WorkAreasMetricsData data = new WorkAreasMetricsData(new Collection<WorkAreaWeight>(requestsService.GetWorkAreasGroupByWeight(projectId)));

            data.GChartWorkAreasByWeightPie = CreateWorkAreasByWeightPieUrl(data);
            data.GChartWorkAreasByRequestPie = CreateWorkAreasByRequestPieUrl(data);
            data.ProjectId = projectId;
            data.ProjectName = Projects.GetProjectName(projectId);
            data.ViewTitle = "Work Areas Metrics";
            RenderView("WorkAreasMetrics", data);
        }

        #region Google Charts helper methods
        private static string CreateWorkAreasByWeightPieUrl(WorkAreasMetricsData data)
        {
            if (data.WorkAreasByWeight.Count() == 0)
            {
                return String.Format(CultureInfo.InvariantCulture, "/{0}/{1}", ConfigurationManager.AppSettings["SiteImagesPath"], "chart_EmptyWeightByWorkAreaPie.png");
            }
            else
            {
                LabeledValuePieChart chartBuilder = new LabeledValuePieChart(400, 150, PieChartType.ThreeD);
                chartBuilder.SetTitle("Weight by workarea");

                List<float> pieData = new List<float>();
                List<string> pieLabels = new List<string>();

                foreach (WorkAreaWeight wa in data.WorkAreasByWeight)
                {
                    pieData.Add(wa.Weight);
                    pieLabels.Add(wa.Name);
                }
                chartBuilder.SetPieChartLabels(pieLabels.ToArray());
                chartBuilder.SetData(pieData.ToArray());

                return chartBuilder.GetUrl();
            }
        }
        private static string CreateWorkAreasByRequestPieUrl(WorkAreasMetricsData data)
        {
            if (data.WorkAreasByWeight.Count() == 0)
            {
                return String.Format(CultureInfo.InvariantCulture, "/{0}/{1}", ConfigurationManager.AppSettings["SiteImagesPath"], "chart_EmptyRequestByWorkAreaPie.png");
            }
            else
            {
                LabeledValuePieChart chartBuilder = new LabeledValuePieChart(400, 150, PieChartType.ThreeD);
                chartBuilder.SetTitle("Request by workarea");

                List<float> pieData = new List<float>();
                List<string> pieLabels = new List<string>();

                foreach (WorkAreaWeight wa in data.WorkAreasByWeight)
                {
                    pieData.Add(wa.Requests);
                    pieLabels.Add(wa.Name);
                }
                chartBuilder.SetPieChartLabels(pieLabels.ToArray());
                chartBuilder.SetData(pieData.ToArray());

                return chartBuilder.GetUrl();
            }
        }
        #endregion
    }
}
